﻿using System;
using System.Linq;
using System.Reflection;
using System.Windows.Forms;
using Data.Common;
using Data.Enum;
using DataAccess.BikeManage.logical;
using DevComponents.DotNetBar;
using WinForm.Analysis;
using WinForm.CarManage;
using WinForm.FinancialManage;
using WinForm.Helper;
using WinForm.Reports;
using WinForm.SystemManage;
using WinForm.UserManage;

namespace WinForm
{
    public partial class FrmNew : Office2007RibbonForm
    {
        public FrmNew()
        {
            InitializeComponent();
            InitializeCurrentUser();
            WindowState = FormWindowState.Maximized;
        }

        private void InitializeCurrentUser()
        {
            lbUserName.Text = SystemInfo.OperateName;
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(AddUser).ToString());
        }

        private void btnCheckUser_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(FrmCheckUser).ToString());
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(ModifyPasswordFrm).ToString());
        }

        private void buttonItem4_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(FrmDispatch).ToString());
        }

        private void buttonItem14_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(FrmUnlock).ToString());
        }

        private void buttonItem15_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(FrmUserList).ToString());
        }

        private void buttonItem16_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(FrmParkingCheck).ToString());
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(AnalysisData).ToString());
        }

        private void buttonItem3_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(FrmCharge).ToString());
        }

        private void buttonItem17_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(FrmParkingAnalysis).ToString());
        }

        private void buttonItem5_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(FrmParkingManage).ToString());
        }

        /// <summary>
        ///     使打开的子窗体唯一
        /// </summary>
        /// <param name="childTypeString"></param>
        private void OpenWindow(string childTypeString)
        {
            try
            {
                //没有存在的子窗体则生成一个子窗体
                if (!ContainMdiChild(childTypeString))
                {
                    // Get current process assembly
                    Assembly assembly = Assembly.GetExecutingAssembly();

                    // Create data type using type string
                    Type typForm = assembly.GetType(childTypeString);

                    if (typForm != null)
                    {
                        // Create object using type's "InvokeMember" method
                        Object obj = typForm.InvokeMember(null, BindingFlags.DeclaredOnly |
                                                                BindingFlags.Public | BindingFlags.NonPublic |
                                                                BindingFlags.Instance | BindingFlags.CreateInstance,
                            null, null, null);

                        // Show child form 
                        if (obj != null)
                        {
                            var myChild = obj as Form;
                            if (myChild != null)
                            {
                                myChild.MdiParent = this;
                                myChild.WindowState = FormWindowState.Maximized;
                                myChild.Show();
                                myChild.Focus();
                            }
                        }
                    }
                }
            }
            catch (Exception e1)
            {
                MyMessageBox.ShowMessageBoxError(e1.Source + e1.Message);
            }
        }


        /// <summary>
        ///     Search mdi child form by specific type string
        /// </summary>
        /// <param name="childTypeString"></param>
        /// <returns></returns>
        private bool ContainMdiChild(string childTypeString)
        {
            try
            {
                Form myMdiChild = MdiChildren.FirstOrDefault(f => f != null && f.GetType().ToString() == childTypeString);

                //// Show the exist form
                //if (myMDIChild != null)
                //{
                //    myMDIChild.TopMost = true;
                //    myMDIChild.Show();
                //    myMDIChild.Focus();
                //    return true;
                //}

                //如果存在子窗体则dispose
                if (myMdiChild != null)
                {
                    myMdiChild.Dispose();
                }

                else
                    return false;
            }
            catch (Exception e1)
            {
                MyMessageBox.ShowMessageBoxError(e1.Source + e1.Message);
            }

            return false;
        }

        private void buttonItem6_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(DbConfig).ToString());
        }

        private void buttonItem7_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(AddVipUser).ToString());
        }

        private void buttonItem8_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(FrmPrivilegeManage).ToString());
        }

        private void buttonItem9_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(FrmChangePassword).ToString());
        }

        private void buttonItem10_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(ReportOperatePanel).ToString());
        }

        private void FrmNew_Load(object sender, EventArgs e)
        {
            //设备没有初始化也可以进入系统，只是和设备相关的功能不能使用
            //初始化设备
            var zq58Logical = new Zq58Logical();
            ResultInfo<string> equip = zq58Logical.CheckEquipment();
            if (equip.ResultNumber == ResultEnum.Success)
            {
                //保存设备信息
                SystemInfo.OperatePsamId = equip.Obj;
            }
            else
            {
                MyMessageBox.ShowMessageBoxInfo("请注意，读卡设备没有连上！");
            }
        }

        /// <summary>
        /// 补卡
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem11_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof (ReSaveCpuCardFrm).ToString());
        }

        /// <summary>
        /// 换卡
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem12_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(ChangeCpuCardFrm).ToString());
        }

        /// <summary>
        /// 充值回退
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonItem13_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(FrmBackCharge).ToString());
        }

        /// <summary>
        /// 关闭系统时进行资产确认
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FrmNew_FormClosed(object sender, FormClosedEventArgs e)
        {
            AssetConfrim assetConfrim = new AssetConfrim(true);
            assetConfrim.ShowDialog();
        }

        private void buttonItem18_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(CardInOutFrm).ToString());
        }

        private void buttonItem19_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(MoneyInOutFrm).ToString());
        }

        private void buttonItem20_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(PrintFinancialFrm).ToString());
        }

        private void buttonItem21_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(ModifyCardInfo).ToString());
        }

        private void buttonItem22_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof (AddUser).ToString());
        }

        private void buttonItem23_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(ReportOperatePanel).ToString());
        }

        private void buttonItem24_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(CardInOutFrm).ToString());
        }

        private void buttonItem25_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(MoneyInOutFrm).ToString());
        }

        private void buttonItem26_Click(object sender, EventArgs e)
        {
            OpenWindow(typeof(PrintFinancialFrm).ToString());
        }
    }
}